#include "qteglobal.h"
#include <QString>

#include <QFile>
#include <iostream>
#include <climits>
#include <list>

namespace QTE
{

QTECORE_EXPORT bool isPureLetters(const QString &str)
{
	auto stdStr = str.toStdString();

	for(const char &c : stdStr)
	{
		if( not ((c >= 'A' and c <= 'Z') or (c >= 'a' and c <= 'z')) )
			return false;
	}
	return true;
}

QTECORE_EXPORT bool isPureAscii(const QString &str)
{
	auto stdStr = str.toStdString();

	for(const char &c : stdStr)
	{
		if( J_SCT(signed char, c) < 0 ) //扩展ASCII的数值大于127，有符号的char会变成负数
			return false;
	}
	return true;
}

QTECORE_EXPORT QString checkFileName(const QString &srcFileName)
{
	if( not QFile::exists(srcFileName) )
		return srcFileName;

	int index = srcFileName.lastIndexOf(".");
	int il = -1, ir = -1;

	ir = srcFileName.lastIndexOf(")", index);
	if( ir > 1 )
	{
		il = srcFileName.lastIndexOf("(", ir);
		if( il < 0 )
			ir = -1;
	}

	index = 0;
	QString fileName;
	QString tmp("(%1)");

	if( il >= 0 and ir > 1 )
	{
		QString name = srcFileName.mid(0, il++);
		int cflag = srcFileName.mid(il + 1, ir - il).toInt();

		int i = cflag - 1;
		for(; i>=0; i--)
		{
			fileName = name + tmp.arg(i);
			if( not QFile::exists(fileName) )
				break;
		}

		if( i < 0 )
		{
			for(i=cflag+1; i>0; i++)
			{
				fileName = name + tmp.arg(i);
				if( not QFile::exists(fileName) )
					break;
			}
			if( i < 0 )
				return QString();
		}
		return fileName;
	}

	for(int i=0; i>=0; i++)
	{
		fileName = srcFileName + tmp.arg(i);
		if( not QFile::exists(fileName) )
			break;
	}
	return fileName;
}

QString version()
{
	return QTE_VERSION_STR;
}

} //namespace QTE
